package com.ts

import android.Manifest
import android.content.Intent
import android.content.pm.PackageManager
import android.opengl.Matrix
import android.os.Bundle
import android.util.Log
import android.widget.Button
import androidx.activity.enableEdgeToEdge
import androidx.appcompat.app.AppCompatActivity
import androidx.core.app.ActivityCompat
import androidx.core.content.ContextCompat
import androidx.core.view.ViewCompat
import androidx.core.view.WindowInsetsCompat
import com.hjq.permissions.OnPermissionCallback
import com.hjq.permissions.Permission
import com.hjq.permissions.XXPermissions
import com.ts.audiocamera.Camera2Activity
import com.ts.audiocamera.FFmepgActivity3
import com.ts.audiocamera.OpenglActivity
import com.ts.audiocamera.R
import com.ts.audiocamera.OpenglRenderActivity
import com.ts.audiocamera.ffmepg.FFmpegVideoActivity
import com.ts.camera2.Camera2Oes.CameraOesActivity
import com.ts.mediacodec.Activity.MediaCodecActivity

class HomeActivity : AppCompatActivity() {
    private  val TAG = "HomeActivity"
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        enableEdgeToEdge()
        setContentView(R.layout.activity_home)
        ViewCompat.setOnApplyWindowInsetsListener(findViewById(R.id.main)) { v, insets ->
            val systemBars = insets.getInsets(WindowInsetsCompat.Type.systemBars())
            v.setPadding(systemBars.left, systemBars.top, systemBars.right, systemBars.bottom)
            insets
        }
// 检查相机权限
        XXPermissions.with(this)
            // 申请单个权限
            .permission(Permission.CAMERA)
            .permission(Permission.MANAGE_EXTERNAL_STORAGE)
            .permission(Permission.RECORD_AUDIO)
//            .permission(Permission.READ_MEDIA_IMAGES)
//            .permission(Permission.READ_MEDIA_VIDEO)
//            .permission(Permission.READ_MEDIA_AUDIO)
            // 申请多个权限

            // 设置权限请求拦截器（局部设置）
            //.interceptor(new PermissionInterceptor())
            // 设置不触发错误检测机制（局部设置）
            //.unchecked()
            .request(object : OnPermissionCallback {

                override fun onGranted(permissions: MutableList<String>, allGranted: Boolean) {
                    if (!allGranted) {
//                        toast("获取部分权限成功，但部分权限未正常授予")
                        return
                    }
//                    toast("获取录音和日历权限成功")
                }

                override fun onDenied(permissions: MutableList<String>, doNotAskAgain: Boolean) {
                    if (doNotAskAgain) {
//                        toast("被永久拒绝授权，请手动授予录音和日历权限")
                        // 如果是被永久拒绝就跳转到应用权限系统设置页面
                        XXPermissions.startPermissionActivity(applicationContext, permissions)
                    } else {
//                        toast("获取录音和日历权限失败")
                    }
                }
            })
        findViewById<Button>(R.id.camera).setOnClickListener {
            startActivity(Intent(this,Camera2Activity::class.java))
        }
        var model1= FloatArray(16)
        Matrix.setIdentityM(model1,0);
        Matrix.scaleM(model1,0,-1f,
            1f,1.0f)
//      Matrix.rotateM(model1,0,model1,0,1.57079f,0.0f,0.0f,1.0f)
        Matrix.rotateM(model1,0,1.57079f,0.0f,0.0f,1.0f)
        Log.e(TAG+"1","\"----------------------------------\"")
        for (i in 0..3) {
            for (l in 0..3) {
                Log.e(TAG+"1", model1[i*4+l].toString())
            }
        }
        Log.e(TAG+"1","\"----------------------------------\"")

        findViewById<Button>(R.id.ffmpeg).setOnClickListener {
            startActivity(Intent(this, FFmepgActivity3::class.java))
        }

        findViewById<Button>(R.id.opengl).setOnClickListener {
            startActivity(Intent(this, OpenglActivity::class.java))
        }

        findViewById<Button>(R.id.oes_camera).setOnClickListener {
            startActivity(Intent(this, CameraOesActivity::class.java))
        }

        findViewById<Button>(R.id.ffmpegvideo).setOnClickListener {
            startActivity(Intent(this,FFmpegVideoActivity::class.java))
        }

        findViewById<Button>(R.id.media_codec).setOnClickListener {
            startActivity(Intent(this,MediaCodecActivity::class.java))
        }


        findViewById<Button>(R.id.idtext).setOnClickListener {
            startActivity(Intent(this, OpenglRenderActivity::class.java))
        }
    }
}